package com.qian.apiserver.service.serviceImpl;

import com.qian.apiserver.entity.GmUser;
import com.qian.apiserver.entity.GmWarn;
import com.qian.apiserver.model.User;
import com.qian.apiserver.model.Warn;
import com.yaowk.api.common.PageReq;
import com.yaowk.api.common.PageResp;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import com.qian.apiserver.service.WarnServiceBase;
import com.qian.apiserver.service.IWarnService;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

@Service
public class WarnServiceImpl extends WarnServiceBase {

    public PageResp<Warn> find(final Warn warn,PageReq pageReq) {
        PageRequest pageRequest = new PageRequest(pageReq.getPageNumber() - 1, pageReq.getPageSize());
        Page<GmWarn> page = getDao().findAll(new Specification<GmWarn>() {
            public Predicate toPredicate(Root<GmWarn> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate condition = cb.isNotNull(root.<Integer>get("id"));
                if(isNotEmpty(warn.getWarnType()))
                    condition = cb.and(condition,cb.like(root.<String>get("warnType"),"%"+warn.getWarnType()+"%"));
                return condition;
            }
        },pageRequest);
        PageResp<Warn> pageResp = page2pageResp(page);
        return pageResp;
    }
}